package aip.ui.information.srvc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;

import aip.core.dao.information.CatalogDao;
import aip.core.model.information.Catalog;
import aip.ui.information.web.spring.CatalogEditForm;
import aip.ui.information.web.spring.CatalogListForm;

import com.googlecode.horncomb.bean.paged.PagedList;
import com.googlecode.horncomb.bean.paged.PagedSetting;
import com.googlecode.horncomb.web.util.TagUtils;

/**
 * Catalog form service
 * <p>
 * 负责人/principal:<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:<br/>
 * @author
 * @version $Id$
 */
public class CatalogFormService
{
    private CatalogDao catalogDao;

    /**
     * Catalog列表
     * @param form
     *        表单对象
     * @return 列表页面数据
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> listReference(CatalogListForm form)
            throws Exception
    {
        Map<String, Object> ref = new HashMap<String, Object>();
        PagedSetting ps = form.getPagedSettingMap().values().iterator().next();
        PagedList<List<? extends Catalog>> result = this.catalogDao.queryAll(ps
                .getOrder(), ps.getPage(), ps.getPageSize());
        List<? extends Catalog> catalogList = this.catalogDao.queryAll(null);
        Map<Integer, Catalog> catalogMap = TagUtils.convertToMap(catalogList,
                "id");
        ref.put("catalogMap", catalogMap);
        ref.put("catalogList", result.getSource()); //$NON-NLS-1$
        ref.put("totalRows", result.getNrOfElements()); //$NON-NLS-1$
        return ref;
    }

    /**
     * 删除选中的项
     * @param form
     *        表单对象
     * @throws Exception
     */
    public void deleteItems(CatalogListForm form) throws Exception
    {
        if (form.getSelectedIds().size() > 0)
        {
            this.catalogDao.deleteByIds(form.getSelectedIds());
        }
    }

    /**
     * 获得Catalog表单主对象
     * @param form
     *        表单对象
     * @throws Exception
     */
    public void get(CatalogEditForm form) throws Exception
    {
        if (form != null && form.getId() > 0)
        {
            Catalog catalog = this.catalogDao.getById(form.getId());
            BeanUtils.copyProperties(catalog, form);
        }
    }

    /**
     * 显示编辑Catalog界面相关数据
     * @param form
     *        表单对象
     * @return 显示页面数据
     * @throws Exception
     */
    public Map<String, Object> showReference(CatalogEditForm form)
            throws Exception
    {
        // other reference data
        Map<String, Object> ref = new HashMap<String, Object>();
        List<? extends Catalog> catalogList = this.catalogDao.queryAll(null);
        ref.put("catalogList", catalogList);
        return ref;
    }

    /**
     * 保存Catalog表单
     * @param form
     *        表单对象
     * @throws Exception
     */
    public void save(CatalogEditForm form) throws Exception
    {
        this.catalogDao.save(form);
    }

    /**
     * 删除单个Catalog
     * @param form
     *        表单对象
     * @throws Exception
     */
    public void deleteItem(CatalogEditForm form) throws Exception
    {
        this.catalogDao.deleteById(form.getId());
    }

    /**
     * 显示编辑Catalog界面相关数据
     * @param form
     *        表单对象
     * @return 显示页面数据
     * @throws Exception
     */
    public Map<String, Object> showCatalog(CatalogEditForm form)
            throws Exception
    {
        // other reference data
        Map<String, Object> ref = new HashMap<String, Object>();
        ref.put("catalogMap", this.catalogDao.getCatalogMap());
        return ref;
    }

    /**
     * @param catalogDao
     *        the catalogDao to set
     */
    public void setCatalogDao(CatalogDao catalogDao)
    {
        this.catalogDao = catalogDao;
    }
}
